using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;

namespace TaskMaster
{
    /// <summary>
    /// Represents information on a time interval in which a task was engaged.
    /// </summary>
    public partial class ActivityRecord
    {
        /// <summary>
        /// Represents the method that will handle the event of the time record being completed.  A time record is considered complete when both the start and end times have been specified.
        /// </summary>
        /// <param name="record">The time record which fired the event.</param>
        public delegate void RecordCompleteHandler(ActivityRecord record);
        public delegate void DurationModifiedHandler(ActivityRecord record, ActivityRecord.DurationModifiedEventArgs arg);


        //private Task task;
        //private DateTime startTime;
        //private DateTime endTime;
        //private ArrayList tags;
        private bool complete = false;

        ///// <summary>
        ///// Initialize an instance of the <c>ActivityRecord</c> class which tracks the specified task.
        ///// </summary>
        ///// <param name="task">A task to record time for.</param>
        //public TimeRecord(Task task)
        //{
        //    this.task = task;
        //}

        ///// <summary>
        ///// The task that a record is being kept of.
        ///// </summary>
        //public Task Task
        //{
        //    get { return task; }
        //    set
        //    {
        //        if (value is Task) { task = value; }
        //        else { throw new Exception("Value is not of type Task"); }
        //    }
        //}

        ///// <summary>
        ///// The date and time the task was started.
        ///// </summary>
        ///// <exception>Exception is thrown if start time comes after the end time.</exception>
        //public DateTime StartTime
        //{
        //    get { return startTime; }
        //    set {
        //        if ((value <= endTime || endTime == DateTime.MinValue) && startTime == DateTime.MinValue) 
        //        { startTime = value; }
        //        else if (value <= endTime || endTime == DateTime.MinValue)
        //        {
        //            TimeSpan durationDiff = startTime - value;
        //            startTime = value;
        //            DurationModifiedEvent(this, new DurationModifiedEventArgs(durationDiff));
        //        }
        //        else
        //        {
        //            Exception ex = new Exception("Start time must be less than equal to end time.");
        //            throw ex;
        //        }
        //    }
        //}

        ///// <summary>
        ///// The date and time the task was ended.
        ///// </summary>
        ///// <exception>Exception is thrown if the end time comes before the start time.</exception>
        //public DateTime EndTime
        //{
        //    get { return endTime; }
        //    set {
        //        if (value > startTime) { 
        //            if (complete) 
        //            {
        //                TimeSpan durationDiff = value - endTime;
        //                endTime = value;
        //                DurationModifiedEvent(this, new DurationModifiedEventArgs(durationDiff)); 
        //            }
        //            else
        //            {
        //                endTime = value;
        //                complete = true;
        //                RecordCompleteEvent(this);
        //            }
        //        }
        //        else
        //        {
        //            Exception ex = new Exception("End time must be greater than or equal to start time.");
        //            throw ex;
        //        }
        //    }
        //}

        private Boolean validateTaskName(string name)
        {
            return true;
        }

        /// <summary>
        /// Calculate the duration that the task was worked on for this time record.
        /// </summary>
        /// <returns>Time span that the task was worked on for this time record.  Returns zero if the end time has not been specified.</returns>
        public TimeSpan TaskDuration()
        {
            TimeSpan duration = EndTime - StartTime;
            // If the duration is negative, then the time record must be incomplete and the end time has not been specified.
            if (duration < TimeSpan.Zero) { duration = TimeSpan.Zero; }
            return duration;
        }

        public void AddTag(string tagName)
        {
            return;
        }

        //Events
        /// <summary>
        /// Triggered when the end time value is set for the time record.
        /// </summary>
        public event RecordCompleteHandler RecordCompleteEvent;
        /// <summary>
        /// Triggered when the start or end time of a completed time record is changed.
        /// </summary>
        public event DurationModifiedHandler DurationModifiedEvent;

        public class DurationModifiedEventArgs : EventArgs
        {
            TimeSpan _durationDiff = TimeSpan.Zero;

            public TimeSpan DurationDifference
            {
                get { return _durationDiff; }
                set { _durationDiff = value; }
            }

            public DurationModifiedEventArgs(TimeSpan durationDiff)
            {
                _durationDiff = durationDiff;
            }
        }
    }
}
